package com.e2oto.seller.modules.seller.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseBean;
import com.e2oto.seller.common.base.BaseMvpActivity;
import com.e2oto.seller.common.utils.ImageCompressUtils;
import com.e2oto.seller.common.utils.ImageLoader;
import com.e2oto.seller.common.utils.LogTools;
import com.e2oto.seller.common.utils.PermissionUtils;
import com.e2oto.seller.common.utils.SPtools;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.launch.bean.LoginBean;
import com.e2oto.seller.modules.launch.ui.LoginActivity;
import com.e2oto.seller.modules.seller.bean.ApplyAdmissionCommitBean;
import com.e2oto.seller.modules.seller.presenter.ShopAptitudePresenter;
import com.e2oto.seller.modules.seller.views.ShopAptitudeView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import me.iwf.photopicker.PhotoPicker;

/**
 * Created by ZhangLiQiang on 2017/11/14.
 */

public class ShopAptitudeActivity extends BaseMvpActivity<ShopAptitudeView, ShopAptitudePresenter> implements ShopAptitudeView {


    @BindView(R.id.shop_aptitude_licence)
    ImageView licenceIv;
    @BindView(R.id.shop_aptitude_id_card_face)
    ImageView idCardFaceIv;
    @BindView(R.id.shop_aptitude_id_card_back)
    ImageView idCardBackIv;
    @BindView(R.id.shop_aptitude_other)
    ImageView otherIv;


    private static final int REQUEST_CODE_LICENCE = 1001;
    private static final int REQUEST_CODE_ID_CARD_FACE = 1002;
    private static final int REQUEST_CODE_ID_CARD_BACK = 1003;
    private static final int REQUEST_CODE_OTHER = 1004;

    private ApplyAdmissionCommitBean commitBean;

    private int currentRequestCode;
    private String licencePath;
    private String idCardFacePath;
    private String idCardBackPath;
    private String otherPath;

    @Override
    protected ShopAptitudePresenter createPresenter() {
        return new ShopAptitudePresenter();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_shop_aptitude;
    }

    @Override
    protected void afterView(Bundle savedInstanceState) {
        super.afterView(savedInstanceState);
        commitBean = (ApplyAdmissionCommitBean) getIntent().getSerializableExtra("commit_bean");
    }

    @OnClick({R.id.shop_aptitude_licence_rl, R.id.shop_aptitude_id_card_face_rl, R.id.shop_aptitude_id_card_back_rl, R.id.shop_aptitude_other_rl})
    public void onClickAddImage(View view) {
        switch (view.getId()) {
            case R.id.shop_aptitude_licence_rl:
                currentRequestCode = REQUEST_CODE_LICENCE;
                break;
            case R.id.shop_aptitude_id_card_face_rl:
                currentRequestCode = REQUEST_CODE_ID_CARD_FACE;
                break;
            case R.id.shop_aptitude_id_card_back_rl:
                currentRequestCode = REQUEST_CODE_ID_CARD_BACK;
                break;
            case R.id.shop_aptitude_other_rl:
                currentRequestCode = REQUEST_CODE_OTHER;
                break;
        }
        PermissionUtils.requestPermissions(ShopAptitudeActivity.this, new PermissionUtils.RequestPermissionCallback() {
            @Override
            public void allow() {
                PhotoPicker.builder()
                        .setPhotoCount(1)
                        .setShowCamera(true)
                        .setShowGif(false)
                        .setPreviewEnabled(false)
                        .start(ShopAptitudeActivity.this, currentRequestCode);
            }
        }, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    }

    @OnClick(R.id.toolbar_left_btn)
    public void onClickBack() {
        finish();
    }

    @OnClick(R.id.shop_aptitude_commit)
    public void onClickCommit() {
        if (TextUtils.isEmpty(licencePath)) {
            showMessage("请上传您的营业执照");
            return;
        }
        if (TextUtils.isEmpty(idCardFacePath)) {
            showMessage("请上传您的身份证(正面)");
            return;
        }
        if (TextUtils.isEmpty(idCardBackPath)) {
            showMessage("请上传您的身份证(背面)");
            return;
        }
        List<String> list = new ArrayList<>();
        list.add(licencePath);
        list.add(idCardFacePath);
        list.add(idCardBackPath);
        list.add(otherPath);
        ImageCompressUtils.compressByRx(new Consumer<List<File>>() {
            @Override
            public void accept(@NonNull List<File> files) throws Exception {
                for (File f : files) {
                    LogTools.e(f.getAbsolutePath());
                }
                commitBean.setPaper_license(files.get(0));
                commitBean.setCard_id_front(files.get(1));
                commitBean.setCard_id_back(files.get(2));
                commitBean.setOther_photo(files.get(3));
                mPresenter.applySeller(commitBean);
            }
        }, list, this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            List<String> images = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            switch (requestCode) {
                case REQUEST_CODE_LICENCE:
                    licencePath = images.get(0);
                    ImageLoader.load(this, licencePath, licenceIv);
                    break;
                case REQUEST_CODE_ID_CARD_FACE:
                    idCardFacePath = images.get(0);
                    ImageLoader.load(this, idCardFacePath, idCardFaceIv);
                    break;
                case REQUEST_CODE_ID_CARD_BACK:
                    idCardBackPath = images.get(0);
                    ImageLoader.load(this, idCardBackPath, idCardBackIv);
                    break;
                case REQUEST_CODE_OTHER:
                    otherPath = images.get(0);
                    ImageLoader.load(this, otherPath, otherIv);
                    break;

            }
        }

    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }

    @Override
    public void applySellerCallback(BaseBean<LoginBean> baseBean) {
        if (baseBean.getCode() == 200) {
            SellerApp.sellerId = baseBean.getData().getSeller_id() + "";
            SellerApp.userId = baseBean.getData().getUser_id() + "";
            SPtools.put(this, "category_id", baseBean.getData().getCategory_id());
            SPtools.put(this, "batch_category_id", baseBean.getData().getBatch_category_id());
            exitApp();
            startActivity(new Intent(this, HomeActivity.class));
            startActivity(new Intent(this, ShopInfoActivity.class));
        } else {

        }
        showMessage(baseBean.getMsg());
    }
}

